package com.senmo.manager;

import android.text.TextUtils;

import com.senmo.model.LoginResult;
import com.senmo.model.User;
import com.senmo.model.event.LoginStateChangeEvent;
import com.senmo.request.DataHull;
import com.senmo.request.RequestFactory;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

import de.greenrobot.event.EventBus;
import lx.af.utils.KV;

/**
 * author: lx
 * date: 16-1-4
 */
public class AccountManager {

    private static AccountManager sInstance = new AccountManager();
    private final Object mLock = new Object();
    private ExecutorService mExecutor = Executors.newSingleThreadExecutor();

    private User mUser;

    public static AccountManager getInstance() {
        return sInstance;
    }

    public void login(final String phone, final String password, final UserCallback c) {
        loginInner(phone, password, c);
    }

    public void autoLogin(final UserCallback c) {
        String phone = KV.getString(Consts.PREF_KEY_PHONE, null);
        if (TextUtils.isEmpty(phone)) {
            if (c != null) {
                c.onFail(UserCallback.USERNAME_INVALID, null);
            }
            return;
        }
        String password = KV.getString(Consts.PREF_KEY_PASSWORD, null);
        if (TextUtils.isEmpty(password)) {
            if (c != null) {
                c.onFail(UserCallback.PASSWORD_INVALID, null);
            }
            return;
        }
        loginInner(phone, password, c);
    }

    public void logout() {
        synchronized (mLock) {
            mUser = null;
            KV.remove(Consts.PREF_KEY_PHONE);
            KV.remove(Consts.PREF_KEY_PASSWORD);
        }
        EventBus.getDefault().post(new LoginStateChangeEvent(false));
    }

    public User getUser() {
        return new User(mUser);
    }

    public boolean isLogin() {
        return mUser != null;
    }

    public int getSessionId() {
        return mUser != null ? mUser.session_id : 0;
    }

    // ==============================================

    private void loginInner(final String phone, final String password, final UserCallback c) {
        mExecutor.execute(new Runnable() {
            @Override
            public void run() {
                synchronized (mLock) {
                    DataHull d = RequestFactory.login(phone, password).request();
                    if (d.isRequestSuccess()) {
                        LoginResult result = d.getParsedData();
                        initUser(phone, password, result.session_id);
                        if (c != null) {
                            c.onSuccess(mUser);
                        }
                        EventBus.getDefault().post(new LoginStateChangeEvent(true));
                    } else {
                        c.onFail(UserCallback.REQUEST_FAIL, d);
                    }
                }
            }
        });
    }

    private void initUser(String phone, String password, int session_id) {
        mUser = new User();
        mUser.phone = phone;
        mUser.password = password;
        mUser.session_id = session_id;
        KV.put(Consts.PREF_KEY_PHONE, phone);
        KV.put(Consts.PREF_KEY_PASSWORD, password);
    }

}
